<html>
<head>
  <title>Evernote Export</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="YXBJ Windows/601935 (zh-CN, DDL); Windows/10.0.0 (Win64); EDAMVersion=V2;"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="660"/>

<div>
<span><div style="min-width: 100%; position: relative;"><div style="outline: 0px; box-sizing: inherit; text-size-adjust: 100%; -webkit-font-smoothing: antialiased; tab-size: 4;"><div style="min-width: 1200px; outline: 0px; box-sizing: inherit; background-color: rgb(245, 246, 247); -webkit-font-smoothing: antialiased; tab-size: 4; zoom: 1;"><div style="outline: 0px; box-sizing: inherit; margin: 0px; padding: 8px 12px 0px; display: flex; -webkit-box-pack: center; justify-content: center; height: 100%;"><div style="outline: 0px; margin: 0px 0px 0px 8px; padding: 0px; box-sizing: border-box; width: 1318px;"><div style="float: right; box-sizing: inherit; outline: 0px; margin: 0px; padding: 0px; width: 1010px;"><div style="outline: 0px; box-sizing: inherit; margin: 0px; position: relative; background: rgb(255, 255, 255); padding: 0px 24px 16px;"><div style="box-sizing: inherit; outline: 0px; margin: 0px; padding: 16px 0px 0px; position: relative;"><div style="outline: 0px; box-sizing: inherit; margin: 0px; padding: 0px; overflow-wrap: break-word;"><div style="outline: 0px; box-sizing: inherit; margin: 0px; padding: 0px; overflow-wrap: break-word; position: relative; min-height: 70px; word-break: break-word;"><div style="outline: 0px; padding: 0px; overflow-wrap: break-word; box-sizing: border-box; margin: 0px 0px 16px;"><div><span style="font-size: 24pt; font-family: 宋体; font-weight: bold;">性能优化(4)-内存优化</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="font-size: 16px; box-sizing: border-box; margin: 0px 0px 16px; padding: 0px; overflow-x: auto; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><span style="font-size: 18px; box-sizing: border-box; overflow-x: auto; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-family: 宋体; font-weight: bold; line-height: 1.8;">本篇文章是码牛学院经网上整理收集给大家的性能优化全集，希望能帮助到各位同学</span></div><div style="font-size: 14px; margin-top: 1em; margin-bottom: 1em;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">因为Android平台和Java语言本身的某些特性的缘故，在开发过程中，如果不注意这些特性。可能会导致内存消耗，比其它平台(IOS)和开发语言(C/C++)多得多。所以，我们需要最大化的去避免，额外的内存开销和泄露。所以，通过了解这些特性，并合理的组织你的代码，来减少APP的内存开销和避免内存泄露，写出高质量的代码，提高APP的运行性能。</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="box-sizing: border-box; font-size: 14pt; color: rgb(204, 0, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;-en-paragraph:true;">OOM</span><span style="font-size: 24px; box-sizing: border-box; color: rgb(204, 0, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;-en-paragraph:true;">：</span></div><div style="font-size: 18px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 18px; box-sizing: border-box; overflow-x: auto; color: rgb(204, 102, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">内存泄露可以引发很多的问题：</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">1.程序卡顿，响应速度慢（内存占用高时JVM虚拟机会频繁触发GC）</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">2.莫名消失（当你的程序所占内存越大，它在后台的时候就越可能被干掉。反之内存占用越小，在后台存在的时间就越长）</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">3.直接崩溃（OutOfMemoryError）</span></div><div style="font-size: 18px; box-sizing: border-box; margin: 0px 0px 8px; padding: 0px; overflow-x: auto; border-width: 0px; list-style: none;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="font-size: 18px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 18px; box-sizing: border-box; overflow-x: auto; color: rgb(204, 102, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">ANDROID内存面临的问题：</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">1.有限的堆内存，原始只有16M</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">2.内存大小消耗等根据设备，操作系统等级，屏幕尺寸的不同而不同</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">3.程序不能直接控制</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">4.支持后台多任务处理（multitasking）</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">5.运行在虚拟机之上</span></div></div><div style="font-size: 14px; box-sizing: border-box; margin: 0px; padding: 0px;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 24px; box-sizing: border-box; overflow-x: auto; color: rgb(204, 0, 0); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">5大步骤：</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">本文主要通过如下的5R方法来对ANDROID内存进行优化：</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 18px; box-sizing: border-box; overflow-x: auto; color: rgb(204, 102, 0); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">1.Reckon（计算）</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">首先需要知道你的app所消耗内存的情况，知己知彼才能百战不殆</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 18px; box-sizing: border-box; overflow-x: auto; color: rgb(204, 102, 0); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">2.Reduce（减少）</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">消耗更少的资源</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 18px; box-sizing: border-box; overflow-x: auto; color: rgb(204, 102, 0); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">3.Reuse（重用）</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">当第一次使用完以后，尽量给其他的使用</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 18px; box-sizing: border-box; overflow-x: auto; color: rgb(204, 102, 0); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">5.Recycle（回收）</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">返回资源给生产流</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 18px; box-sizing: border-box; overflow-x: auto; color: rgb(204, 102, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">4.Review（检查）</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; white-space: pre; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">回顾检查你的程序，看看设计或代码有什么不合理的地方。</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="color: rgb(204, 0, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-size: 14pt; font-weight: bold; text-align: center;">Reduce ：</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 18px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">Reduce的意思就是减少，直接减少内存的使用是最有效的优化方式。</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 18px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">下面来看看有哪些方法可以减少内存使用：</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="font-size: 14px; box-sizing: border-box; margin: 0px; padding: 0px;"><span style="font-size: 24px; box-sizing: border-box; color: rgb(153, 102, 51); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">Bitmap</span><span style="font-size: 24px; box-sizing: border-box; color: rgb(153, 102, 51); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">：</span></div><div style="font-size: 14px; box-sizing: border-box; margin: 0px; padding: 0px;"><span style="font-size: 18px; box-sizing: border-box; color: rgb(79, 79, 79); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">Bitmap是内存消耗大户，绝大多数的OOM崩溃都是在操作Bitmap时产生的，下面来看看如何几个处理图片的方法：</span></div><div style="font-size: 14px; box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">图片显示：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">我们需要根据需求去加载图片的大小。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">例如在列表中仅用于预览时加载缩略图（thumbnails ）。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">只有当用户点击具体条目想看详细信息的时候，这时另启动一个fragment／activity／对话框等等，去显示整个图片</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">图片大小：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;" title="Page 19"><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">直接使用ImageView显示bitmap会占用较多资源，特别是图片较大的时候，可能导致崩溃。 </span></div><div><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">使用</span><span style="box-sizing: border-box; font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">BitmapFactory.Options</span><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">设置inSampleSize, 这样做可以减少对系统资源的要求。 </span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;-en-paragraph:true;">属性值inSampleSize表示缩略图大小为原始图片大小的几分之一，即如果这个值为2，则取出的缩略图的宽和高都是原始图片的1/2，图片大小就为原始大小的1/4。</span></div><div style="box-sizing: border-box; padding: 1px 0px 0px; font-size: 12px; width: 936.531px; overflow: auto; background-color: rgb(231, 229, 220); margin: 18px 0px !important;"><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">BitmapFactory.Options bitmapFactoryOptions = </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">new</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> BitmapFactory.Options();  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">bitmapFactoryOptions.inJustDecodeBounds = </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">true</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">bitmapFactoryOptions.inSampleSize = </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(192, 0, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">2</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 这里一定要将其设置回false，因为之前我们将其设置成了true  </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 设置inJustDecodeBounds为true后，decodeFile并不分配空间，即，BitmapFactory解码出来的Bitmap为Null,但可计算出原始图片的长度和宽度  </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">options.inJustDecodeBounds = </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">false</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">Bitmap bmp = BitmapFactory.decodeFile(sourceBitmap, options);  </span></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">图片像素：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;" title="Page 20"><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">Android中图片有四种属性，分别是：</span></div><div><span style="box-sizing: border-box; font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">ALPHA_8：</span><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">每个像素占用1byte内存 </span></div><div><span style="box-sizing: border-box; font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">ARGB_4444：</span><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">每个像素占用2byte内存 </span></div><div><span style="box-sizing: border-box; font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">ARGB_8888：</span><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">每个像素占用4byte内存 （默认）</span></div><div><span style="box-sizing: border-box; font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">RGB_565：</span><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">每个像素占用2byte内存 </span></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">Android默认的颜色模式为ARGB_8888，这个颜色模式色彩最细腻，显示质量最高。但同样的，占用的内存也最大。 所以在对图片效果不是特别高的情况下使用RGB_565（565没有透明度属性），如下：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; padding: 1px 0px 0px; font-size: 12px; width: 936.531px; overflow: auto; background-color: rgb(231, 229, 220); margin: 18px 0px !important;"><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">publicstaticBitmapreadBitMap(Contextcontext, intresId) {  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    BitmapFactory.Optionsopt = newBitmapFactory.Options();  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    opt.inPreferredConfig = Bitmap.Config.RGB_565;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    opt.inPurgeable = </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">true</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    opt.inInputShareable = </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">true</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">//获取资源图片 </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    InputStreamis = context.getResources().openRawResource(resId);  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    returnBitmapFactory.decodeStream(is, </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">null</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">, opt);  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">}  </span></div></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px;" title="Page 20"><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 25.2px;">图片回收：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">使用Bitmap过后，就需要及时的调用</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">Bitmap.recycle()</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">方法来释放Bitmap占用的内存空间，而不要等Android系统来进行释放。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">下面是释放Bitmap的示例代码片段。</span></div><div style="box-sizing: border-box; padding: 1px 0px 0px; font-size: 12px; width: 936.531px; overflow: auto; background-color: rgb(231, 229, 220); margin: 18px 0px !important;"><div style="box-sizing: border-box; margin: 0px; padding: 0px 0px 0px 45px;"><div style="box-sizing: border-box; margin: 0px; padding: 3px 8px 10px 10px; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><br/></div></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 先判断是否已经回收</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">if</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">(bitmap != </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">null</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> &amp;&amp; !bitmap.isRecycled()){  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 回收并且置为null</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    bitmap.recycle();  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    bitmap = </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">null</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">}  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">System.gc();  </span></div></div><div><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">捕获异常：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">经过上面这些优化后还会存在报OOM的风险，所以下面需要一道最后的关卡——捕获OOM异常：</span></div><div style="box-sizing: border-box; padding: 1px 0px 0px; font-size: 12px; width: 936.531px; overflow: auto; background-color: rgb(231, 229, 220); margin: 18px 0px !important;"><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">Bitmap bitmap = </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">null</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">try</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> {  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 实例化Bitmap</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    bitmap = BitmapFactory.decodeFile(path);  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">} </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">catch</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> (OutOfMemoryError e) {  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 捕获OutOfMemoryError，避免直接崩溃</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">}  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">if</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> (bitmap == </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">null</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">) {  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 如果实例化失败 返回默认的Bitmap对象</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">return</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> defaultBitmapMap;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">}  </span></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div></div></div></div></div></div></div></div></div></div></div><div style="font-size: 14px;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 24px; box-sizing: border-box; overflow-x: auto; color: rgb(153, 102, 51); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">修改对象引用类型：</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 18px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">引用类型：</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">引用分为四种级别，这四种级别由高到低依次为：强引用&gt;软引用&gt;弱引用&gt;虚引用。</span></div><div style="font-size: 14px;"><span style="font-size: 14px; box-sizing: border-box; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">强引用（strong reference）</span></div><div style="font-size: 14px; margin-top: 1em; margin-bottom: 1em;"><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;-en-paragraph:true;">如：Object object=new Object（），object就是一个强引用了。当内存空间不足，Java虚拟机宁愿抛出OutOfMemoryError错误，使程序异常终止，也不会靠随意回收具有强引用的对象来解决内存不足问题。</span></div><div style="font-size: 14px;"><span style="font-size: 14px; box-sizing: border-box; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">软引用（SoftReference）</span></div><div style="font-size: 14px; margin-top: 1em; margin-bottom: 1em;"><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;-en-paragraph:true;">只有内存不够时才回收,常用于缓存；当内存达到一个阀值，GC就会去回收它；</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">弱引用（WeakReference）   </span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">弱引用的对象拥有更短暂的生命周期。在垃圾回收器线程扫描它 所管辖的内存区域的过程中，一旦发现了只具有弱引用的对象，不管当前内存空间足够与否，都会回收它的内存。 </span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">虚引用（PhantomReference）</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">   </span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">&quot;虚引用&quot;顾名思义，就是形同虚设，与其他几种引用都不同，虚引用并不会决定对象的生命周期。如果一个对象仅持有虚引用，那么它就和没有任何引用一样，在任何时候都可能被垃圾回收。  </span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 18px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">软引用和弱引用的应用实例：</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">注意</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">：对于</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">SoftReference</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">(</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">软引用</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">)或者</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">WeakReference</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">(</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">弱引用</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">)的Bitmap缓存方案，现在已经不推荐使用了。自</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">Android2.3</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">版本(</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">API Level 9</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">)开始，垃圾回收器更着重于对软/弱引用的回收，</span><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(255, 102, 102); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">所以下面的内容可以选择忽略。</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 5px 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">在Android应用的开发中，为了防止内存溢出，在处理一些占用内存大而且声明周期较长的对象时候，可以尽量应用软引用和弱引用技术。</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 5px 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">下面以使用软引用为例来详细说明（弱引用的使用方式与软引用是类似的）：</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 5px 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">假设我们的应用会用到大量的默认图片，而且这些图片很多地方会用到。如果每次都去读取图片，由于读取文件需要硬件操作，速度较慢，会导致性能较低。所以我们考虑将图片缓存起来，需要的时候直接从内存中读取。但是，由于图片占用内存空间比较大，缓存很多图片需要很多的内存，就可能比较容易发生OutOfMemory异常。这时，我们可以考虑使用软引用技术来避免这个问题发生。</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 5px 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">首先定义一个HashMap，保存软引用对象。</span></div><div style="font-size: 12px; box-sizing: border-box; padding: 1px 0px 0px; width: 936.531px; overflow: auto; background-color: rgb(231, 229, 220); margin: 18px 0px !important;"><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">private</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> Map&lt;String, SoftReference&lt;Bitmap&gt;&gt; imageCache = </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">new</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> HashMap&lt;String, SoftReference&lt;Bitmap&gt;&gt;();  </span></div></div><div style="font-size: 14px;"><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">再来定义一个方法，保存Bitmap的软引用到HashMap。</span></div><div style="font-size: 12px; box-sizing: border-box; padding: 1px 0px 0px; width: 936.531px; overflow: auto; background-color: rgb(231, 229, 220); margin: 18px 0px !important;"><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">public</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">void</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> addBitmapToCache(String path) {  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">       </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 强引用的Bitmap对象</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">       Bitmap bitmap = BitmapFactory.decodeFile(path);  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">       </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 软引用的Bitmap对象</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">       SoftReference&lt;Bitmap&gt; softBitmap = </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">new</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> SoftReference&lt;Bitmap&gt;(bitmap);  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">       </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 添加该对象到Map中使其缓存</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">       imageCache.put(path, softBitmap);  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">   }  </span></div></div><div style="font-size: 14px;"><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">获取的时候，可以通过SoftReference的get()方法得到Bitmap对象。</span></div><div style="font-size: 12px; box-sizing: border-box; padding: 1px 0px 0px; width: 936.531px; overflow: auto; background-color: rgb(231, 229, 220); margin: 18px 0px !important;"><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">public</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> Bitmap getBitmapByPath(String path) {  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 从缓存中取软引用的Bitmap对象</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        SoftReference&lt;Bitmap&gt; softBitmap = imageCache.get(path);  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 判断是否存在软引用</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">if</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> (softBitmap == </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">null</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">) {  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">            </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">return</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">null</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        }  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 取出Bitmap对象，如果由于内存不足Bitmap被回收，将取得空</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        Bitmap bitmap = softBitmap.get();  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">return</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> bitmap;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    }  </span></div></div><div style="font-size: 14px;"><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">使用软引用以后，在OutOfMemory异常发生之前，这些缓存的图片资源的内存空间可以被释放掉的，从而避免内存达到上限，避免Crash发生。</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">需要注意的是，在垃圾回收器对这个Java对象回收前，SoftReference类所提供的get方法会返回Java对象的强引用，一旦垃圾线程回收该Java对象之后，get方法将返回null。所以在获取软引用对象的代码中，一定要判断是否为null，以免出现NullPointerException异常导致应用崩溃。</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 5px 0px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 5px 0px; overflow-x: auto;"><span style="font-size: 18px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">到底什么时候使用软引用，什么时候使用弱引用呢？</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 5px 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">个人认为，如果只是想避免OutOfMemory异常的发生，则可以使用软引用。如果对于应用的性能更在意，想尽快回收一些占用内存比较大的对象，则可以使用弱引用。</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 5px 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">还有就是可以根据对象是否经常使用来判断。如果该对象可能会经常使用的，就尽量用软引用。如果该对象不被使用的可能性更大些，就可以用弱引用。</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 5px 0px; overflow-x: auto;"><span style="font-size: 16px; box-sizing: border-box; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">另外，和弱引用功能类似的是WeakHashMap。WeakHashMap对于一个给定的键，其映射的存在并不阻止垃圾回收器对该键的回收，回收以后，其条目从映射中有效地移除。WeakHashMap使用ReferenceQueue实现的这种机制。</span></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 5px 0px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="font-size: 16px; box-sizing: border-box; margin: 0px; padding: 5px 0px; overflow-x: auto;"><span style="font-size: 24px; box-sizing: border-box; overflow-x: auto; color: rgb(153, 102, 51); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">其他小tips：</span></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><div style="box-sizing: border-box; margin: 0px; padding: 5px 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">对常量使用static final修饰符</span></div><div style="box-sizing: border-box; margin: 0px; padding: 5px 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">让我们来看看这两段在类前面的声明：</span></div><div><span style="box-sizing: border-box; font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">static </span><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">int intVal = 42;</span></div><div><span style="box-sizing: border-box; font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">static </span><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">String strVal = &quot;Hello, world!&quot;;</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;-en-paragraph:true;">编译器会生成一个叫做clinit的初始化类的方法，当类第一次被使用的时候这个方法会被执行。方法会将42赋给intVal，然后把一个指向类中常量表 的引用赋给strVal。当以后要用到这些值的时候，会在成员变量表中查找到他们。 下面我们做些改进，使用“final”关键字：</span></div><div><span style="box-sizing: border-box; font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">static final </span><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">int intVal = 42;</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="box-sizing: border-box; font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;-en-paragraph:true;">static final </span><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;-en-paragraph:true;">String strVal = &quot;Hello, world!&quot;;</span></div><div style="box-sizing: border-box; margin: 0px; padding: 5px 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">现在，类不再需要clinit方法，因为在成员变量初始化的时候，会将常量直接保存到类文件中。用到intVal的代码被直接替换成42，而使用strVal的会指向一个字符串常量，而不是使用成员变量。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 5px 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">将一个方法或类声明为final不会带来性能的提升，但是会帮助编译器优化代码。举例说，如果编译器知道一个getter方法不会被重载，那么编译器会对其采用内联调用。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 5px 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">你也可以将本地变量声明为final，同样，这也不会带来性能的提升。使用“final”只能使本地变量看起来更清晰些（但是也有些时候这是必须的，比如在使用匿名内部类的时候）。</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">静态方法代替虚拟方法</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">如果不需要访问某对象的字段，将方法设置为静态，调用会加速15%到20%。这也是一种好的做法，因为你可以从方法声明中看出调用该方法不需要更新此对象的状态。</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">减少不必要的全局变量</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">尽量避免static成员变量引用资源耗费过多的实例,比如Context</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">因为Context的引用超过它本身的生命周期，会导致Context泄漏。所以尽量使用Application这种Context类型。 你可以通过调用Context.getApplicationContext()或 Activity.getApplication()轻松得到Application对象。 </span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 21px;">避免创建不必要的对象</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">最常见的例子就是当你要频繁操作一个字符串时，使用StringBuffer代替String。</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">对于所有所有基本类型的组合：int数组比Integer数组好，这也概括了一个基本事实，两个平行的int数组比</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;"> (</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">int</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">,</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">int</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">)</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">对象数组性能要好很多。</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">总体来说，就是避免创建短命的临时对象。减少对象的创建就能减少垃圾收集，进而减少对用户体验的影响。</span></div><div><span style="box-sizing: border-box; font-size: 18px; color: rgb(79, 79, 79); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">避免内部Getters/Setters</span></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 14px; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><div><span style="font-size: 14px; color: rgb(79, 79, 79); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">在Android中，虚方法调用的代价比直接字段访问高昂许多。通常根据面向对象语言的实践，在公共接口中使用Getters和Setters是有道理的，但在一个字段经常被访问的类中宜采用直接访问。</span></div><div><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div></div><div style="box-sizing: border-box; margin: 0px 0px 16px; padding: 0px; font-size: 16px; overflow-x: auto; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);"><div style="box-sizing: border-box; margin: 0px; padding: 0px;" title="Page 19"><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px;" title="Page 20"><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px;" title="Page 20"><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; background-color: rgb(255, 255, 255); color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 21px;">避免使用浮点数</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">通常的经验是，在Android设备中，浮点数会比整型慢两倍。</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 21px;">使用实体类比接口好</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">假设你有一个HashMap对象，你可以将它声明为HashMap或者Map：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 13px; white-space: pre-wrap; word-break: break-all; overflow-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px;"><div><span style="font-size: 13px; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 1.42857;">Map map1 = new HashMap();</span></div><div><span style="font-size: 13px; color: rgb(51, 51, 51); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 1.42857;">HashMap map2 = new HashMap();</span></div></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">哪个更好呢？</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">按照传统的观点Map会更好些，因为这样你可以改变他的具体实现类，只要这个类继承自Map接口。传统的观点对于传统的程序是正确的，但是它并不适合嵌入式系统。调用一个接口的引用会比调用实体类的引用多花费一倍的时间。如果HashMap完全适合你的程序，那么使用Map就没有什么价值。如果有些地方你不能确定，先避免使用Map，剩下的交给IDE提供的重构功能好了。(当然公共API是一个例外：一个好的API常常会牺牲一些性能）</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 21px;">避免使用枚举</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">枚举变量非常方便，但不幸的是它会牺牲执行的速度和并大幅增加文件体积。</span></div><div><span style="font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">使用枚举变量可以让你的API更出色，并能提供编译时的检查。所以在通常的时候你毫无疑问应该为公共API选择枚举变量。但是当性能方面有所限制的时候，你就应该避免这种做法了。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 21px;">for循环</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">访问成员变量比访问本地变量慢得多，如下面一段代码：</span></div><div style="box-sizing: border-box; padding: 1px 0px 0px; font-size: 12px; width: 936.531px; overflow: auto; background-color: rgb(231, 229, 220); margin: 18px 0px !important;"><div style="box-sizing: border-box; margin: 0px; padding: 0px 0px 0px 45px;"><div style="box-sizing: border-box; margin: 0px; padding: 3px 8px 10px 10px; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><span style="box-sizing: border-box; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248); color: silver; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: normal;">[java]</span><span style="box-sizing: border-box; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248); color: silver; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;"> </span><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="view plain">view plain</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="copy">copy</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="print">print</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="?">?</a></div></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">for</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">(</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">int</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> i =</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(192, 0, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">0</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">; i &lt; </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">this</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">.mCount; i++)  {}  </span></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">永远不要在for的第二个条件中调用任何方法，如下面一段代码：</span></div><div style="box-sizing: border-box; padding: 1px 0px 0px; font-size: 12px; width: 936.531px; overflow: auto; background-color: rgb(231, 229, 220); margin: 18px 0px !important;"><div style="box-sizing: border-box; margin: 0px; padding: 0px 0px 0px 45px;"><div style="box-sizing: border-box; margin: 0px; padding: 3px 8px 10px 10px; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><span style="box-sizing: border-box; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248); color: silver; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: normal;">[java]</span><span style="box-sizing: border-box; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248); color: silver; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;"> </span><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="view plain">view plain</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="copy">copy</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="print">print</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="?">?</a></div></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">for</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">(</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">int</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> i =</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(192, 0, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">0</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">; i &lt; </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">this</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">.getCount(); i++) {}  </span></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">对上面两个例子最好改为：</span></div><div style="box-sizing: border-box; padding: 1px 0px 0px; font-size: 12px; width: 936.531px; overflow: auto; background-color: rgb(231, 229, 220); margin: 18px 0px !important;"><div style="box-sizing: border-box; margin: 0px; padding: 0px 0px 0px 45px;"><div style="box-sizing: border-box; margin: 0px; padding: 3px 8px 10px 10px; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><span style="box-sizing: border-box; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248); color: silver; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: normal;">[java]</span><span style="box-sizing: border-box; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248); color: silver; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;"> </span><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="view plain">view plain</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="copy">copy</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="print">print</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="?">?</a></div></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">int</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> count = </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">this</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">.mCount; / </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">int</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> count = </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">this</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">.getCount();  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">for</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">(</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">int</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> i =</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(192, 0, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">0</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">; i &lt; count; i++)  {}  </span></div></div><div><span style="font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">在java1.5中引入的for-each语法。编译器会将对数组的引用和数组的长度保存到本地变量中，这对访问数组元素非常好。 但是编译器还会在每次循环中产生一个额外的对本地变量的存储操作</span><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 21px;">（如下面例子中的变量a）</span><span style="font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">，这样会比普通循环多出4个字节，速度要稍微慢一些：</span></div><div style="box-sizing: border-box; padding: 1px 0px 0px; font-size: 12px; width: 936.531px; overflow: auto; background-color: rgb(231, 229, 220); margin: 18px 0px !important;"><div style="box-sizing: border-box; margin: 0px; padding: 0px 0px 0px 45px;"><div style="box-sizing: border-box; margin: 0px; padding: 3px 8px 10px 10px; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><span style="box-sizing: border-box; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248); color: silver; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: normal;">[java]</span><span style="box-sizing: border-box; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248); color: silver; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;"> </span><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="view plain">view plain</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="copy">copy</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="print">print</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="?">?</a></div></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">for</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> (Foo a : mArray) {  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    sum += a.mSplat;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">}  </span></div></div><div><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">了解并使用类库</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">选择Library中的代码而非自己重写，除了通常的那些原因外，考虑到系统空闲时会用汇编代码调用来替代library方法，这可能比JIT中生成的等价的最好的Java代码还要好。</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">当你在处理字串的时候，不要吝惜使用</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">String.indexOf()</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">，</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">String.lastIndexOf()</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">等特殊实现的方法。这些方法都是使用C/C++实现的，比起Java循环快10到100倍。</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">System.arraycopy</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">方法在有JIT的Nexus One上，自行编码的循环快9倍。</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">android.text.format包下的</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">Formatter</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">类，提供了IP地址转换、文件大小转换等方法；DateFormat类，提供了各种时间转换，都是非常高效的方法。</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">TextUtils</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">类，对于字符串处理Android为我们提供了一个简单实用的TextUtils类，如果处理比较简单的内容不用去思考正则表达式不妨试试这个在android.text.TextUtils的类</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">高性能</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">MemoryFile</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">类，很多人抱怨Android处理底层I/O性能不是很理想，如果不想使用NDK则可以通过MemoryFile类实现高性能的文件读写操作。MemoryFile适用于哪些地方呢？对于I/O需要频繁操作的，主要是和外部存储相关的I/O操作，MemoryFile通过将 NAND或SD卡上的文件，分段映射到内存中进行修改处理，这样就用高速的RAM代替了ROM或SD卡，性能自然提高不少，对于Android手机而言同时还减少了电量消耗。该类实现的功能不是很多，直接从Object上继承，通过JNI的方式直接在C底层执行。</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto; text-align: center;"><span style="box-sizing: border-box; font-size: 32px; overflow-x: auto; color: rgb(204, 0, 0); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">Reuse:</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">Reuse重用，减少内存消耗的重要手段之一。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">核心思路就是将已经存在的内存资源重新使用而避免去创建新的，最典型的使用就是</span><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">缓存（Cache</span><span style="box-sizing: border-box; font-size: 16px; color: rgb(67, 67, 67); font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 24px;">）</span><span style="box-sizing: border-box; font-size: 16px; color: rgb(67, 67, 67); font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 24px;">和</span><span style="box-sizing: border-box; font-size: 16px; color: rgb(67, 67, 67); font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 24px;">池（Pool）</span><span style="box-sizing: border-box; font-size: 16px; color: rgb(67, 67, 67); font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 24px;">。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 24px; color: rgb(153, 102, 51); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 24px;">Bitmap缓存：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">Bitmap缓存分为两种：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(67, 67, 67); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">一种是内存缓存，一种是硬盘缓存。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">内存缓存（LruCache）：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">以牺牲宝贵的应用内存为代价，内存缓存提供了快速的Bitmap访问方式。系统提供的</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">LruCache</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">类是非常适合用作缓存Bitmap任务的，它将最近被引用到的对象存储在一个强引用的</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">LinkedHashMap</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">中，并且在缓存超过了指定大小之后将最近不常使用的对象释放掉。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(229, 102, 0); font-family: 微软雅黑, Verdana, sans-serif, 宋体; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">注意</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(67, 67, 67); font-family: 微软雅黑, Verdana, sans-serif, 宋体; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">：</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">以前有一个非常流行的内存缓存实现是</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">SoftReference</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">(</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">软引用</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">)或者</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">WeakReference</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">(</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">弱引用</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">)的Bitmap缓存方案，然而现在已经不推荐使用了。自</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">Android2.3</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">版本(</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">API Level 9</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">)开始，垃圾回收器更着重于对软/弱引用的回收，这使得上述的方案相当无效。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">硬盘缓存（DiskLruCache）：</span></div><div style="box-sizing: border-box; margin: 0px 0px 15px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">一个内存缓存对加速访问最近浏览过的Bitmap非常有帮助，但是你不能局限于内存中的可用图片。GridView这样有着更大的数据集的组件可以很轻易消耗掉内存缓存。你的应用有可能在执行其他任务(如打电话)的时候被打断，并且在后台的任务有可能被杀死或者缓存被释放。一旦用户重新聚焦(</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">resume</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">)到你的应用，你得再次处理每一张图片。</span></div><div style="box-sizing: border-box; margin: 0px 0px 15px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">在这种情况下，硬盘缓存可以用来存储Bitmap并在图片被内存缓存释放后减小图片加载的时间(次数)。当然，从硬盘加载图片比内存要慢，并且应该在后台线程进行，因为硬盘读取的时间是不可预知的。</span></div><div style="box-sizing: border-box; margin: 0px 0px 15px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(229, 102, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">注意</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(67, 67, 67); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">：如果访问图片的次数非常频繁，那么</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(0, 102, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">ContentProvider</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(67, 67, 67); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">可能更适合用来存储缓存图片，例如</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(0, 102, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">Image Gallery</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(67, 67, 67); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">这样的应用程序。</span></div><div><span style="font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">更多关于内存缓存和硬盘缓存的内容请看Google官方教程</span><a href="https://developer.android.com/develop/index.html" style="box-sizing: border-box; background: transparent; outline: 0px; font-size: 16px; color: rgb(202, 0, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">https://developer.android.com/develop/index.html</a></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 24px; color: rgb(153, 102, 51); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">图片缓存的开源项目：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 16px; color: rgb(67, 67, 67); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 24px;">对于图片的缓存现在都倾向于使用开源项目，这里我列出几个我搜到的：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px 0px 5px; padding: 0px; font-size: 16px; overflow-x: auto; border: 0px; list-style: none; overflow-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; border: 0px; overflow-wrap: normal; word-break: normal; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 21px;">1. Android-Universal-Image-Loader 图片缓存</span></div><div><span style="font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">目前使用最广泛的图片缓存，支持主流图片缓存的绝大多数特性。</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;-en-paragraph:true;">项目地址：</span><a href="https://github.com/nostra13/Android-Universal-Image-Loader" style="font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;-en-paragraph:true;">https://github.com/nostra13/Android-Universal-Image-Loader</a></div><div style="box-sizing: border-box; margin: 0px 0px 5px; padding: 0px; font-size: 16px; overflow-x: auto; border: 0px; list-style: none; overflow-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; border: 0px; overflow-wrap: normal; word-break: normal; color: rgb(85, 85, 85); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 21px;"> </span></div><div><span style="box-sizing: border-box; font-size: 18px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">2. picasso square开源的图片缓存</span></div><div><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">项目地址：</span><a href="https://github.com/square/picasso" style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">https://github.com/square/picasso</a></div><div><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">特点：(1)可以自动检测adapter的重用并取消之前的下载</span></div><div><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">(2)图片变换</span></div><div><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">(3)可以加载本地资源</span></div><div><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">(4)可以设置占位资源</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;-en-paragraph:true;">(5)支持debug模式</span></div><div style="box-sizing: border-box; margin: 0px 0px 5px; padding: 0px; font-size: 16px; overflow-x: auto; border: 0px; list-style: none; overflow-wrap: normal; word-break: normal;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; border: 0px; overflow-wrap: normal; word-break: normal; color: rgb(85, 85, 85); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 21px;"> </span></div><div><span style="box-sizing: border-box; font-size: 18px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">3. ImageCache 图片缓存，包含内存和Sdcard缓存</span></div><div><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">项目地址：</span><a href="https://github.com/Trinea/AndroidCommon" style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">https://github.com/Trinea/AndroidCommon</a></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;-en-paragraph:true;">特点：</span></div><div><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">(1)支持预取新图片，支持等待队列</span></div><div><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">(2)包含二级缓存，可自定义文件名保存规则</span></div><div><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">(3)可选择多种缓存算法(FIFO、LIFO、LRU、MRU、LFU、MFU等13种)或自定义缓存算法</span></div><div><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">(4)可方便的保存及初始化恢复数据</span></div><div><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">(5)支持不同类型网络处理</span></div><div style="margin-top: 1em; margin-bottom: 1em;"><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;-en-paragraph:true;">(6)可根据系统配置初始化缓存等</span></div><div><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 18px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 24px;">4. Android 网络通信框架Volley</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">项目地址：</span><a href="https://android.googlesource.com/platform/frameworks/volley" style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">https://android.googlesource.com/platform/frameworks/volley</a></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">我们在程序中需要和网络通信的时候，大体使用的东西莫过于AsyncTaskLoader，HttpURLConnection，AsyncTask，HTTPClient（Apache）等，在2013年的Google I/O发布了Volley。Volley是Android平台上的网络通信库，能使网络通信更快，更简单，更健壮。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 21px;">特点：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 21px;">(1)</span><span style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">JSON，图像等的异步下载；</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 21px;">(2)</span><span style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">网络请求的排序（scheduling）</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 21px;">(3)</span><span style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">网络请求的优先级处理</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 21px;">(4)</span><span style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">缓存</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 21px;">(5)</span><span style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">多级别取消请求</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: simsun; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 21px;">(6)</span><span style="box-sizing: border-box; orphans: 2; widows: 2; font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">和Activity和生命周期的联动（Activity结束时同时取消所有网络请求）</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 24px; color: rgb(153, 102, 51); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 25.2px;">Adapter适配器</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">在Android中Adapter使用十分广泛，特别是在list中。所以adapter是数据的</span><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;"> “集散地”</span><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;"> ，所以对其进行内存优化是很有必要的。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">下面算是一个标准的使用模版：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">主要使用convertView和ViewHolder来进行缓存处理</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; padding: 1px 0px 0px; font-size: 12px; width: 936.531px; overflow: auto; background-color: rgb(231, 229, 220); margin: 18px 0px !important;"><div style="box-sizing: border-box; margin: 0px; padding: 0px 0px 0px 45px;"><div style="box-sizing: border-box; margin: 0px; padding: 3px 8px 10px 10px; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><span style="box-sizing: border-box; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248); color: silver; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: normal;">[java]</span><span style="box-sizing: border-box; font-size: 9px; border-left: 3px solid rgb(153, 153, 153); background-color: rgb(248, 248, 248); color: silver; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;"> </span><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="view plain">view plain</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="copy">copy</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="print">print</a><a href="http://blog.csdn.net/a396901990/article/details/38707007#" style="box-sizing: border-box; font-size: 9px; background-color: rgb(248, 248, 248); outline: 0px; border: none; color: rgb(160, 160, 160); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: normal;" title="?">?</a></div></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(100, 100, 100); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">@Override</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">public</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> View getView(</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">int</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> position, View convertView, ViewGroup parent) {  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    ViewHolder vHolder = </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">null</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">//如果convertView对象为空则创建新对象，不为空则复用  </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">if</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> (convertView == </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">null</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">) {  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        convertView = inflater.inflate(..., </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">null</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">);  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 创建 ViewHodler 对象  </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        vHolder = </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">new</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> ViewHolder();  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        vHolder.img= (ImageView) convertView.findViewById(...);  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        vHolder.tv= (TextView) convertView.findViewById(...);  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 将ViewHodler保存到Tag中(Tag可以接收Object类型对象，所以任何东西都可以保存在其中)</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        convertView.setTag(vHolder);  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    } </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">else</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> {  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">//当convertView不为空时，通过getTag()得到View  </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">        vHolder = (ViewHolder) convertView.getTag();  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    }  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">// 给对象赋值，修改显示的值  </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    vHolder.img.setImageBitmap(...);  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    vHolder.tv.setText(...);  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">return</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> convertView;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">}  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">//将显示的View 包装成类  </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">static</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> </span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: blue; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 18px;">class</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;"> ViewHolder {  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    TextView tv;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">    ImageView img;  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">}  </span></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div></div><div><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 24px; color: rgb(153, 102, 51); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 24px;">池（PooL）</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 18px; overflow-x: auto; color: rgb(75, 75, 75); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 1.5;">对象池：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">对象池使用的基本思路是：将用过的对象保存起来，等下一次需要这种对象的时候，再拿出来重复使用，从而在一定程度上减少频繁创建对象所造成的开销。 并非所有对象都适合拿来池化――因为维护对象池也要造成一定开销。对生成时开销不大的对象进行池化，反而可能会出现“维护对象池的开销”大于“生成新对象的开销”，从而使性能降低的情况。但是对于生成时开销可观的对象，池化技术就是提高性能的有效策略了。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 18px; color: rgb(68, 68, 68); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 21px;">线程池：</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">线程池的基本思想还是一种对象池的思想，开辟一块内存空间，里面存放了众多(未死亡)的线程，池中线程执行调度由池管理器来处理。当有线程任务时，从池中取一个，执行完成后线程对象归池，这样可以避免反复创建线程对象所带来的性能开销，节省了系统的资源。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">比如：一个应用要和网络打交道，有很多步骤需要访问网络，为了不阻塞主线程，每个步骤都创建个线程，在线程中和网络交互，用线程池就变的简单，线程池是对线程的一种封装，让线程用起来更加简便，只需要创一个线程池，把这些步骤像任务一样放进线程池，在程序销毁时只要调用线程池的销毁函数即可。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">java提供了</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">ExecutorService</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">和</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">Executors</span><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">类，我们可以应用它去建立线程池。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;">通常可以建立如下4种：</span></div><div style="box-sizing: border-box; padding: 1px 0px 0px; font-size: 12px; width: 936.531px; overflow: auto; background-color: rgb(231, 229, 220); margin: 18px 0px !important;"><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">/** 每次只执行一个任务的线程池 */</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">ExecutorService singleTaskExecutor =  Executors.newSingleThreadExecutor();  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">/** 每次执行限定个数个任务的线程池 */</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">ExecutorService limitedTaskExecutor = Executors.newFixedThreadPool(</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(192, 0, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">3</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">);  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">/** 所有任务都一次性开始的线程池 */</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">ExecutorService allTaskExecutor = Executors.newCachedThreadPool();  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(0, 130, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">/** 创建一个可在指定时间里执行任务的线程池，亦可重复执行 */</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">  </span></div><div style="box-sizing: border-box; list-style: decimal-leading-zero outside; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); background-color: rgb(245, 250, 226); margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">ExecutorService scheduledTaskExecutor = Executors.newScheduledThreadPool(</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: rgb(192, 0, 0); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">3</span><span style="box-sizing: border-box; background-color: rgb(245, 250, 226); border: none; font-size: 12px; color: black; font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 18px;">);  </span></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px; font-size: 16px; overflow-x: auto;"> </div><div><br style="box-sizing: border-box; margin: 0px; padding: 0px;"/></div></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 24px; color: rgb(153, 102, 51); font-family: &quot;Microsoft YaHei&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold; line-height: 26px;">注意：</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 21px;">要根据情况适度使用缓存，因为内存有限。</span></div><div style="box-sizing: border-box; margin: 10px auto; padding: 0px; font-size: 16px; overflow-x: auto;"><span style="box-sizing: border-box; font-size: 16px; overflow-x: auto; color: rgb(77, 77, 77); font-family: Verdana, Arial, Helvetica, sans-serif; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 21px;">能保存路径地址的就不要存放图片数据，不经常使用的尽量不要缓存，不用时就清空。</span></div><div style="box-sizing: border-box; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; font-size: 16px; color: rgb(77, 77, 77); font-family: &quot;Microsoft YaHei&quot;, Arial; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 26px;"> </span></div></div></div></div></div></div></div></div></div></div><div><br/></div></div></div></div></div></div></div></div></div></div></div></div></div><div><br/></div></span>
</div></body></html> 